package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.demo.StoreShow;
import com.stn.zjx.givingtreasure.fragment.main.MainIndexFragment;
import com.stn.zjx.givingtreasure.utils.ToolsUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StoreItemListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public List<StoreShow> dataBeanList = new ArrayList<>();
    private LayoutInflater inflater;
    public BaseActivity context;

    public StoreItemListAdapter(BaseActivity context) {
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_store_show, parent, false));
        return vh;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        IndexHolder ih = (IndexHolder) viewHolder;
        StoreShow data = dataBeanList.get(position);
        Glide.with(context)
                .load(data.getImg())
                .apply(new RequestOptions().fitCenter())
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(ih.storeIv);
          ih.storeName.setText(data.getTitle());
          List<String>  tag=data.getTag();
            if(tag!=null&&tag.size()!=0){
                   String str="";
                   for (int i=0;i<tag.size();i++){
                        String s=tag.get(i);
                       str=str+s;
                        if(i!=tag.size()-1){
                            str=str+" |";
                        }
                   }

                ih.storeTag.setText(str);
            }else{
                ih.storeTag.setText("");
            }

        switch (data.getSendType()){
            case 0:
                ih.sendServiceTag.setVisibility(View.GONE);
                break;
            case 1:
                ih.sendServiceTag.setText("到店送");
                ih.sendServiceTag.setVisibility(View.VISIBLE);
                break;
            case 2:
                ih.sendServiceTag.setText("限量送");
                ih.sendServiceTag.setVisibility(View.VISIBLE);
                break;
            case 3:
                ih.sendServiceTag.setText("折扣送");
                ih.sendServiceTag.setVisibility(View.VISIBLE);
                break;
            case 4:
                ih.sendServiceTag.setText("体验送");
                ih.sendServiceTag.setVisibility(View.VISIBLE);
                break;
           case 5:
                ih.sendServiceTag.setText("买就送");
                ih.sendServiceTag.setVisibility(View.VISIBLE);
                break;
            case 6:
                ih.sendServiceTag.setText("分享送");
                ih.sendServiceTag.setVisibility(View.VISIBLE);
                break;
        }
        switch (data.getIsBaoyou()){
            case  0:
                ih.freeShippingTv.setText("");
                ih.freeShippingTv.setVisibility(View.GONE);
                break;
            case  1:
                ih.freeShippingTv.setText("包邮");
                ih.freeShippingTv.setVisibility(View.VISIBLE);
                break;
        }
            ih.nowPriceTv.setText("¥"+data.getNowPrice());
            ih.originPriceTv.setText("¥"+data.getOriginPrice());
            ih.evaluateTv.setText(ToolsUtils.getWancount(data.getGuestCount())+"条评价"+"  "+data.getGuestRate()+"好评率");
            if(data.getShop()!=null){
                ih.shopName.setText(""+data.getShop().getShopName());
                ih.shopCityTv.setText(data.getShop().getShopCity());
                ih.goShopTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                });
            }

    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    public void refreshList(List<StoreShow> dataBeans) {
        dataBeanList.removeAll(dataBeanList);
        dataBeanList.addAll(dataBeans);
        notifyDataSetChanged();
    }

    public void appendList(List<StoreShow> dataBeans) {
        dataBeanList.addAll(dataBeans);
        notifyDataSetChanged();
    }

    public void delete(int position) {
        dataBeanList.remove(position);
        notifyDataSetChanged();
    }

    public void clearList() {
        dataBeanList.removeAll(this.dataBeanList);
        notifyDataSetChanged();
    }


    public class IndexHolder extends RecyclerView.ViewHolder {

        public View rootView;
        @BindView(R.id.store_iv)
        ImageView storeIv;
        @BindView(R.id.store_name)
        TextView storeName;
        @BindView(R.id.store_tag)
        TextView storeTag;
        @BindView(R.id.send_service_tag)
        TextView sendServiceTag;
        @BindView(R.id.free_shipping_tv)
        TextView freeShippingTv;
        @BindView(R.id.now_price_tv)
        TextView nowPriceTv;
        @BindView(R.id.origin_price_tv)
        TextView originPriceTv;
        @BindView(R.id.evaluate_tv)
        TextView evaluateTv;
        @BindView(R.id.shop_name)
        TextView shopName;
        @BindView(R.id.go_shop_tv)
        TextView goShopTv;
        @BindView(R.id.shop_city_tv)
        TextView shopCityTv;
        IndexHolder(@NonNull View itemView) {
            super(itemView);
            rootView = itemView;
            ButterKnife.bind(this, itemView);
            originPriceTv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);  // 设置中划线并加清晰
        }
    }



}